//
//  RHSelEventTypeController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/14.
//

#import "RHSelEventTypeController.h"
#import "RHEventTypeModel.h"

@interface RHSelEventTypeController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;

@property (nonatomic ,assign) __block NSInteger selectIndex;

@end

@implementation RHSelEventTypeController


- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"日志类型"];
    [self.view setBackgroundColor:CFTabViewBgColor];
    _selectIndex = -1;
    [self creatData];
    [self setupUI];
}

- (void)creatData{
    
    NSArray *type_array = @[@0,@1,@2];
    NSArray *type_name_array = @[@"日志信息",@"告警",@"错误"];
    
    [type_array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RHEventTypeModel *model = [[RHEventTypeModel alloc] init];
        model.eventType = [obj integerValue];
        model.eventName = type_name_array[idx];
        [self.dataArray addObject:model];
    }];
    
}

- (void)setModel:(RHEventTypeModel *)model{
    _model = model;
    kWeakSelf;
    [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RHEventTypeModel *type_model = obj;
        if(type_model.eventType == model.eventType){
            weakSelf.selectIndex = idx;
        }
    }];
}

- (void)setupUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark - UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    return footView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    [cell.textLabel setTextColor:indexPath.row == _selectIndex?CFMainColor:kCOLOR_HEX(@"666666")];
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:indexPath.row == _selectIndex?[UIImage imageNamed:@"submit_success"]:nil forState:UIControlStateNormal];
    [btn setTitleColor:kCOLOR_HEX(@"666666") forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [btn setImagePosition:QMUIButtonImagePositionRight];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [btn setFrame:CGRectMake(0, 0, 23, 15)];
    [cell setAccessoryView:btn];
    
    RHEventTypeModel *model = self.dataArray[indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@",model.eventName]];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectIndex = indexPath.row;
    [tableView reloadData];
    
    RHEventTypeModel *model = self.dataArray[indexPath.row];
    if(self.callBackBlock){
        self.callBackBlock(model);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}



@end
